package com.ym521.opencv

import android.graphics.Bitmap
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.camera.core.CameraSelector
import androidx.camera.core.ImageAnalysis
import androidx.camera.core.Preview
import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.core.content.ContextCompat
import androidx.lifecycle.LifecycleOwner
import com.ym521.facecheck.FaceCheck
import com.ym521.facecheck.callback.FaceCheckCallback
import com.ym521.facecheck.camerax.SimpleImageAnalyzer
import com.ym521.opencv.databinding.FaceCheckActivityBinding
import java.util.concurrent.Executors

/**
 *@author Ym
 *E-mail: 2435970206@qq.com
 *createTime:2024/6/8
 *explain:
 *
 */
class FaceCheckActivity : AppCompatActivity() {
    private val TAG = "FaceCheckActivity"
    private lateinit var viewBind: FaceCheckActivityBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewBind = FaceCheckActivityBinding.inflate(layoutInflater)
        setContentView(viewBind.root)
        FaceCheck.install(applicationContext)
            .logEnabled()
            .memoryEnabled(true)
            .visualizeEnabled(true)
            .faceCheckListener(
                object : FaceCheckCallback {
                    override fun onCheckResult(success: Boolean, angle: Int, bitmap: Bitmap?) {
                        Log.w(TAG, "人脸检测：$success  angle:$angle ")
                        bitmap?.let {
                            viewBind.ivPerView.setImageBitmap(it)
                        }
                    }
                })

    }

    override fun onResume() {
        super.onResume()
        startCamera()
    }

    private fun startCamera() {
        val cameraProviderFuture = ProcessCameraProvider.getInstance(this)
        cameraProviderFuture.addListener({
            val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()
            // 预览用例
            val preview = Preview
                .Builder()
                .build()
            preview.setSurfaceProvider(viewBind.pvOpenCV.surfaceProvider)
            // 可以根据需要设置目标分辨率和格式，以优化性能
            val imageAnalysis = ImageAnalysis
                .Builder()
                .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST) // 保持最新一帧
                .setOutputImageFormat(ImageAnalysis.OUTPUT_IMAGE_FORMAT_RGBA_8888)
                .build()
            val simpleImageAnalyzer = SimpleImageAnalyzer.build()
            imageAnalysis.setAnalyzer(Executors.newSingleThreadExecutor(), simpleImageAnalyzer)
            // 绑定到生命周期
            val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
            try {
                cameraProvider.unbindAll()
                cameraProvider.bindToLifecycle(
                    this as LifecycleOwner,
                    cameraSelector,
                    preview,
                    imageAnalysis
                )
            } catch (exc: Exception) {
                Log.e(TAG, "Use case binding failed", exc)
            }
        }, ContextCompat.getMainExecutor(this))
    }
}